package study.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

/**
 * 参数相关的测试
 *
 *
 * @author Ychiha
 * @version 1.0
 * @date 31/12/2021 下午12:08
 */
@Controller
public class ParamsController {

    //用以判断
    private static String judge = "type";

    @RequestMapping("/param")
    public ModelAndView param(
            //接收h5中id为did的数据 defaultValue = "0" 表示属性如果为空时，以0代替
            @RequestParam(value = "did",defaultValue = "0" ) int id,
            //接受h5中id为type的数据 required = false (表示该属性可以为空)
            @RequestParam(value = "type",required = false ) String type
    )    //测试url : http://localhost:8080/param?did=2
         //测试url : http://localhost:8080/param?type=type
    {
        System.out.println("@RequestParam...");
        System.out.println("id = "+id);
        ModelAndView view = new ModelAndView("params");

        //传值
        view.addObject("did",id);
        if("type".equals(type)){
            System.out.println("type = "+type);
            view.addObject("type",type);
        }

        return view;
    }

    @RequestMapping("/path/{did}/{type}")
    public ModelAndView params(
            //接收h5中id为did的数据 defaultValue = "0" 表示属性如果为空时，以0代替
            @PathVariable(value = "did") int id,
            //接受h5中id为type的数据 required = false (表示该属性可以为空)
            @PathVariable(value = "type",required = false ) String type
    )
    //测试url : http://localhost:8080/path/1/2
    {
        System.out.println("@PathVariable");
        System.out.println("id = "+id);
        ModelAndView view = new ModelAndView("params");

        //传值
        view.addObject("did",id);
        if("type".equals(type)){
            System.out.println("type = "+type);
            view.addObject("type",type);
        }

        return view;
    }
}
